///////////////////////////////////////////////////////////
//  Item.cpp
//  Implementation of the Class Item
//  Created on:      19-Aug-2013 10:26:35 PM
//  Original author: Jakkaphan Aiemcharoenjit
///////////////////////////////////////////////////////////

#include "Item.h"
#include "BoolData.h"
#include "IntData.h"
#include "DoubleData.h"
#include "StringData.h"
#include "DataSchemaXML.h"

using namespace Data;

Item::Item(const String_t& name, IData::DataType _type)
{
  this->m_type = IObject::ITEM_T;
  this->m_name = name;
  this->m_comment = _T("");
  this->m_parent = NULL;
  
  switch(_type)
  {
  case IData::BOOL_T:
    this->m_data = new BoolData();
    break;
  case IData::INT_T:
    this->m_data = new IntData(-1);
    break;
  case IData::DOUBLE_T:
    this->m_data = new DoubleData();
    break;
  case IData::STRING_T:
    this->m_data = new StringData();
    break;
  default:
    this->m_data = NULL;
    break;
  }
}


Item::Item(const Item& anotherItem)
{
  this->m_type = IObject::ITEM_T;
  this->m_name = anotherItem.m_name;
  this->m_comment = anotherItem.m_comment;
  this->m_data = static_cast<IData*>(anotherItem.m_data->Clone());
  this->m_parent = anotherItem.m_parent;
}


Item::~Item()
{
  delete this->m_data;
  this->m_data = NULL;
  this->m_parent = NULL;
}


IObject::ObjectType Item::GetType() const 
{

	return this->m_type;
}


String_t Item::GetName() const 
{

	return  this->m_name;
}


IData* Item::GetData() const 
{

	return  this->m_data;
}


String_t Item::GetComment() const 
{

	return  this->m_comment;
}


void Item::SetName(const String_t& name)
{
  this->m_name = name;
}


void Item::SetData(IData* data)
{
  this->m_data = data;
}


void Item::SetComment(const String_t& comment)
{
  this->m_comment = comment;
}


IObject* Item::Clone()
{
  Item* cloneItem = new Item(*this);
	return cloneItem;
}


String_t Item::ToXML()
{
  // <Item name="example">
  //   <Comment>This is a comment.</Comment>
  //   <UseInteger>0</UseInterger>
  // </Item>

  String_t xml = _T("<") + DataSchemaXML::ITEM_ELEMENT;
  String_t context = this->m_name;
  replaceTextXML(context);
  if(!this->m_name.empty())
    xml += _T(" ") + DataSchemaXML::NAME_ATTR + _T("=\"") + context + _T("\">");
  else
    xml += _T(">");

  context = this->m_comment;
  replaceTextXML(context);
  if(!this->m_comment.empty())
    xml += _T("<") + DataSchemaXML::COMMENT_ELEMENT + _T(">") + context + _T("</") + DataSchemaXML::COMMENT_ELEMENT + _T(">");

  xml += this->m_data->ToXML();
  xml += _T("</") + DataSchemaXML::ITEM_ELEMENT + _T(">");

	return  xml;
}


IObject* Item::GetParent()
{
  return  this->m_parent;
}


void Item::SetParent(IObject* parent)
{
  this->m_parent = parent;
}